package dao;

import entity.Trans_1;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class TransDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TransDAO.class);

	public void save(Trans_1 transientInstance) {
		log.debug("saving Trans instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Trans_1 persistentInstance) {
		log.debug("deleting Trans instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Trans_1 findById(entity.TransId id) {
		log.debug("getting Trans instance with id: " + id);
		try {
			Trans_1 instance = (Trans_1) getSession().get("entity.Trans", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Trans_1> findByExample(Trans_1 instance) {
		log.debug("finding Trans instance by example");
		try {
			List<Trans_1> results = (List<Trans_1>) getSession()
					.createCriteria("entity.Trans").add(create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Trans instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Trans as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Trans instances");
		try {
			String queryString = "from Trans";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
        
        public List findByNumero(String num) {
		log.debug("finding all Trans instances");
		try { getSession().clear();
			String queryString = "from Trans_1 where numero='"+num+"' order by time DESC";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
        public int findByEtatCount(String num,String s) {
		log.debug("finding all Trans instances");
		try { getSession().clear();
			String queryString = "select count(*) from Trans_1 where etat like'"+num+"' and date like'"+s+"'";
			int i = ((Long)getSession().createQuery(queryString).uniqueResult()).intValue();
			return i;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Trans_1 merge(Trans_1 detachedInstance) {
		log.debug("merging Trans instance");
		try {
			Trans_1 result = (Trans_1) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Trans_1 instance) {
		log.debug("attaching dirty Trans instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Trans_1 instance) {
		log.debug("attaching clean Trans instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}